using System.Collections.Generic;
using System.Linq;
using OpenQA.Selenium;

namespace YatFram.Browser.Selenium
{
    public class SeleniumMultipleOptions : IMultipleOptions
    {
        private readonly IEnumerable<IWebElement> checkboxOptions;

        public SeleniumMultipleOptions(IEnumerable<IWebElement> checkboxOptions)
        {
            this.checkboxOptions = checkboxOptions;
        }

        public void Uncheck(string value)
        {
            if (CheckedValues.Contains(value))
            {
                checkboxOptions.Single(option => option.Value == value).Toggle();
            }
        }

        public IEnumerable<string> CheckedValues
        {
            get
            {
                var checkedOptions = checkboxOptions.Where(option => option.Selected);
                return checkedOptions.Count() == 0 ? new string[] { } : checkedOptions.Select(option => option.Value);

            }
        }

        public void Check(string value)
        {
            checkboxOptions.Single(option => option.Value == value).Selected = true;
        }

    }
}